package org.aeon.client.view.components.button;

import lombok.extern.slf4j.Slf4j;
import org.aeon.client.context.GlobalContext;
import org.aeon.client.view.components.sessionPanel.GroupInfoPanel;
import org.aeon.client.view.components.sessionPanel.SessionPanel;
import org.aeon.client.view.components.sessionPanel.UserInfoPanel;
import org.aeon.constant.SessionType;
import org.aeon.entity.dto.GroupInfoDto;
import org.aeon.entity.dto.UserInfoDto;
import org.aeon.message.request.GroupInfoRequestMessage;
import org.aeon.message.request.UserInfoRequestMessage;
import org.aeon.utils.SimpleRequestUtil;

import javax.swing.*;
import java.awt.*;

import static org.aeon.client.context.MessageMap.MessageContextKey.GroupInfo;
import static org.aeon.client.context.MessageMap.MessageContextKey.UserInfo;


/**
 * @author: MissingAeon
 * @date: 2024/2/20 16:16
 * @description:
 */
@Slf4j
public class ContactButton extends JButton {
    private final JLabel nameLabel;
    private final SessionPanel sessionPanel;

    public ContactButton(String name, SessionPanel sessionPanel) {
        this.sessionPanel = sessionPanel;
        this.setPreferredSize(new Dimension(274, 60));
        this.setContentAreaFilled(false);

        JPanel butContent = new JPanel(null); // 使用绝对布局
        // butContent.setBackground(Color.RED);

        // 创建姓名标签
        nameLabel = new JLabel(name);
        nameLabel.setBounds(5, 5, 180, 25); // 设置姓名标签的位置和大小
        nameLabel.setFont(new Font("黑体", Font.BOLD, 16));
        nameLabel.setHorizontalAlignment(SwingConstants.LEFT); // 文字左对齐

        // 向面板中添加组件
        butContent.add(nameLabel);

        this.setLayout(new BorderLayout()); // 设置按钮的布局管理器为边界布局
        this.add(butContent, BorderLayout.CENTER); // 将面板添加到按钮的中心位置
    }

    public void setAdditionalInfo(Long id, SessionType sessionType) {
        this.addActionListener(e -> {
            final GlobalContext context = GlobalContext.getContext();
            switch (sessionType) {
                case USER_SESSION -> {
                    final UserInfoRequestMessage reqMsg = new UserInfoRequestMessage();
                    reqMsg.setMessage(id);
                    SimpleRequestUtil.sendAndWait(context, reqMsg, () -> {
                        final UserInfoDto userInfo = context.getMessageMap().del(UserInfo);
                        SwingUtilities.invokeLater(() -> {
                            final UserInfoPanel panel = new UserInfoPanel(sessionPanel);
                            panel.fillInData(userInfo);
                            sessionPanel.add(panel);
                        });
                    });
                }
                case GROUP_SESSION -> {
                    final GroupInfoRequestMessage reqMsg = new GroupInfoRequestMessage();
                    reqMsg.setMessage(id);
                    SimpleRequestUtil.sendAndWait(context, reqMsg, () -> {
                        final GroupInfoDto groupInfo = context.getMessageMap().del(GroupInfo);
                        SwingUtilities.invokeLater(() -> {
                            final GroupInfoPanel panel = new GroupInfoPanel(sessionPanel);
                            panel.fillInData(groupInfo);
                            sessionPanel.add(panel);
                        });
                    });
                }
            }
        });
    }

    /**
     * 修改按钮显示名称
     *
     * @param text
     */
    public void setNameText(String text) {
        nameLabel.setText(text);
    }

    /**
     * 减淡颜色
     */
    public void setColorFade() {
        final Color color = new Color(0xA4A4A4);
        nameLabel.setForeground(color);
    }

}
